Add examples on how to profile a pipeline#13356
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
stevhliu
approved these changes
Mar 30, 2026
Member
stevhliu
left a comment
There was a problem hiding this comment.
super educational, i enjoyed reading this a lot!
- maybe rename "Approach" to something like "How the tooling works" because it describes how it works rather than what the user should do
- it seems like "Afterwards" may be more effective as a blog post as it tells a story about issues 1 and 2 in the "What to look for" section
- could be useful to add a link to this doc from our torch.compile docs
|
|
||
| To inspect this: zoom into a single denoising step, select a CUDA kernel on the GPU row, and look at the corresponding CPU-side launch slice directly above it. The horizontal offset between them is the launch latency. In a healthy trace, CPU launch slices should be well ahead of GPU execution (the CPU is "feeding" the GPU faster than it can consume). | ||
|
|
||
| ### Quick checklist per pipeline |
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
dg845
reviewed
Apr 2, 2026
dg845
reviewed
Apr 2, 2026
dg845
reviewed
Apr 2, 2026
Collaborator
dg845
left a comment
There was a problem hiding this comment.
Thanks for the PR! Left some questions/suggestions :).
Co-authored-by: dg845 <58458699+dg845@users.noreply.github.com>
dg845
reviewed
Apr 3, 2026
dg845
reviewed
Apr 3, 2026
Co-authored-by: dg845 <58458699+dg845@users.noreply.github.com>
Co-authored-by: dg845 <58458699+dg845@users.noreply.github.com>
Member
Author
|
@dg845 thanks for the comments, very helpful. I think you also advocated for the end user which is exactly what I was looking for in the reviews. I have made the changes as requested. PTAL. |
dg845
reviewed
Apr 3, 2026
Co-authored-by: dg845 <58458699+dg845@users.noreply.github.com>
varaprasadtarunkumar
added a commit
to varaprasadtarunkumar/diffusers
that referenced
this pull request
Apr 3, 2026
…-step DtoH sync
When zero_cond_t=True, the modulate_index tensor was being recreated on
every transformer forward pass (once per denoising step) using:
torch.tensor(list_comprehension, device=timestep.device, ...)
This triggers a Python list comprehension + torch.tensor() from a Python
list, which causes a cudaMemcpyAsync + cudaStreamSynchronize (DtoH sync)
that forces the CPU to wait for all pending GPU kernels.
Since img_shapes (which fully determines modulate_index) is fixed for the
entire inference run, the resulting tensor is identical across all steps.
We cache it in _modulate_index_cache keyed by (img_shapes, device), so
the tensor is built only on the first step and reused thereafter.
This eliminates N-1 unnecessary torch.tensor() constructions and DtoH
syncs during inference (where N = num_inference_steps).
This issue was identified in the profiling guide added in huggingface#13356 and
referenced in huggingface#13401.
Follows the same caching pattern as _compute_video_freqs in QwenEmbedRope.
2 tasks
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
TL;DR: Adds a guide on how to profile a pipeline and fix issues like CPU overhead, CPU<->GPU syncs, etc.
Motivation
Since we provide first-class
torch.compilesupport, it's important that our pipelines are set up for optimal success with it. This includes spotting any obvious issues that plague thetorch.compileperformance -- CPU overhead, CPU<->GPU syncs, graphbreaks, kernel launch delays, etc.The best way to spot these bugs is to profile a pipeline, as it gives a granular measurement of where the GPU is spending time and if it is doing so in an expected manner. We can then uncover any unexpected issues and eventually fix them.
Workflow
The README.md added in the PR has all the descriptions, but in summary:
With this Workflow, I was able to fix some issues in the Flux2 Klein pipeline and the Wan pipeline. All changes look quite harmless to me.
Plan
Not only is it helpful to profile pipelines to get a ceiling on performance, but the community could also help us improve our pipelines should this workflow prove to be useful.
Note to reviewers
Please review the changes in
src/diffusers/*. And you can skip straight to the "Afterwards" section in the README.md document.The tutorial is currently available here. Some inline comments.